package cn.com.thinvent.dispatch.web.controller;

import cn.com.thinvent.dispatch.entity.ModelDatasource;
import cn.com.thinvent.dispatch.entity.file.fileShowVo;
import cn.com.thinvent.dispatch.entity.response.*;
import cn.com.thinvent.dispatch.service.biz.DataSourceService;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.sql.SQLException;

/**
 * 结构化数据源管理
 */
@Api(description = "数据源模块")
@Controller
@RequestMapping("/dataSource")
@CrossOrigin(origins = "*")
@ResponseBody
public class DataSourceController {

    @Autowired
    private DataSourceService dataSourceService;

    /**
     * 结构化数据源列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ReturnModel list(@RequestParam("pageNum") Integer pageNum,
                            @RequestParam("pageSize") Integer pageSize) {
        PagedGridResult<ModelDatasource> list = dataSourceService.list(pageNum, pageSize);
        return new ReturnModel(200, list, "");
    }

    @GetMapping("/listUnStruct")
    @ResponseBody
    public ReturnModel listUnStruct(@RequestParam("pageNum") Integer pageNum,
                            @RequestParam("pageSize") Integer pageSize,
                                    @RequestParam("tagId") Integer tagId) {
        PagedGridResult<fileShowVo> list = dataSourceService.listUnStruct(pageNum, pageSize, tagId);
        return new ReturnModel(200, list, "");
    }
    @GetMapping("/delUnStruct")
    @ResponseBody
    public ReturnModel delUnStruct(@RequestParam("dId") Integer dId, @RequestParam("filepath") String filePath) {
        dataSourceService.delUnStruct(dId, filePath);
        return new ReturnModel(200, null, "");
    }


    /**
     * 新增或修改结构化数据源信息
     * @param modelDatasource
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public ReturnModel saveOrUpdate(@RequestBody ModelDatasource modelDatasource) {
        ModelDatasource datasource = dataSourceService.saveOrUpdate(modelDatasource);
        return new ReturnModel(200, datasource, "");
    }

    /**
     * 获取数据源表结构
     * @param modelDatasource
     * @return
     */
    @GetMapping("/getTables")
    @ResponseBody
    public ReturnModel getTables(ModelDatasource modelDatasource) throws SQLException {
        System.out.println(modelDatasource.getDataTable());
        if(modelDatasource.getDatastore() != null && modelDatasource.getDatastore().equals("")) modelDatasource.setDatastore(null);

        TablesResponse tables = dataSourceService.getTables(modelDatasource);
        return new ReturnModel(200, tables, "");
    }

    /**
     * 获取数据库表字段
     * @param modelDatasource
     * @return
     */
    @GetMapping("/getTableFields")
    @ResponseBody
    public ReturnModel getTableFields(ModelDatasource modelDatasource) throws SQLException {
        TableFieldsResponse fields = dataSourceService.getTableFields(modelDatasource);
        return new ReturnModel(200, fields, "");
    }

    /**
     * 数据源详情
     * @param datasourceId
     * @return
     */
    @GetMapping("/detail")
    @ResponseBody
    public ReturnModel detail(@Param("datasourceId") Integer datasourceId, @Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize) throws SQLException, IOException, ClassNotFoundException {
        DataSourceDetailResponse dataSourceDetailResponse = dataSourceService.detail(datasourceId, pageNum, pageSize);
        return new ReturnModel(200, dataSourceDetailResponse, "");
    }

}
